// 引入gulp
const { pipe, dest, src, watch, series } = require('gulp');

// 引入js相关的插件
// const concatjs = require('gulp-concat');
// const uglify = reuqire('gulp-uglify');
const rename = require('gulp-rename');

// // 引入css相关插件
const concatCss = require('gulp-concat-css');
// const cleanName = require('gulp-clean-css');
const scss = require('gulp-sass')(require('sass'));

// 引入HTML相关插件
// const htmlmin = require('gulp-htmlmin');

// 自定义插件
const through2 = require('through2');

// 编译sass第一级目录中文件
function sass() {
    return src('./src/scss/*.scss')
        .pipe(scss())
        .pipe(dest('./dist/css'));
}
function sassBase() {
    return src('./src/scss/base/*.scss')
        .pipe(scss())
        .pipe(dest('./dist/css/module'));
}

// 将scss文件下的base文件中sass编译到dist 下的css目录
function base() {
    return src('./src/scss/base/base.scss')
        .pipe(scss())
        .pipe(dest('./dist/css/base'));
}

// 将js文件全部赋值到dist中去
function js() {
    return src('./src/js/*.js')
        .pipe(dest('./dist/js/'));
}
function commonJs () {
    return src('./src/js/module/*.js')
        .pipe(dest('./dist/js/module'))
}

// 将index.html根页面编译
function index() {
    return src('./src/index.html')
        .pipe(dest('./dist'));
}

// 将dist目录下，将src下面的HTML中common复制到dist目录中
function commonHtml() {
    return src('./src/html/common/*.html')
        .pipe(dest('./dist/html/common'));
}
function html () {
    return src('./src/html/*.html')
        .pipe(dest('./dist/html'));
}
function mainHtml () {
    return src('./src/*.html')
        .pipe(dest('./dist'));
}

// 默认将css从src目录中的common赋值到dist的common中去
function css() {
    return src('./src/css/common/*.css')
    .pipe(dest('./dist/css/common'))
}

// 进行css路径的合并的重写
function rewriteUrl(callback) {
    return through2.obj(function (...args) {
        let [file , encode, cb] = args;
        let source = file.contents.toString();
        let target = callback(source);

        file.contents = Buffer.from(target);
        cb(null, file);
    })
}

// 合并css中header与footer到common中去
function reCss() {
    return src(['./src/css/common/header.css', './src/css/common/footer.css'])
        .pipe(rewriteUrl(function (res) {
            return res.replace(/\.\/src/g, '.');
        }))
        .pipe(concatCss('common.css'))
        .pipe(dest('./dist/css/common'));
}

// 将src下的resource资源加载到dist目录中
function static () {
    return src('./src/resource/*/*')
        .pipe(dest('./dist/resource/'));
}

// login的scss编译
function login () {
    return src('./src/scss/module/login.scss')
        .pipe(scss())
        .pipe(dest('./dist/css/base'))
}
// 将所有需要的fonts字体编译到dist fonts中
function fonts () {
    return src('./src/fonts/*')
        .pipe(dest('./dist/fonts/'));
}
// 将所有的main中的css样式合并到main.css中去
function main () {
    return src(['./src/css/common/recomendList.css', './dist/css/common/common.css'])
        .pipe(concatCss('common.css'))
        .pipe(dest('./dist/css/common'));
}
function video () {
    return src('./src/fonts/video/*')
        .pipe(dest('./dist/fonts/video'))
}

// 导出接口呀
exports.video = video;
exports.sassBase = sassBase;
exports.commonJs = commonJs;
exports.mainHtml = mainHtml;
exports.main = main;
exports.fonts = fonts;
exports.index = index;
exports.login = login;
exports.static = static;
exports.base = base;
exports.commonHtml = commonHtml;
exports.reCss = reCss;
exports.css = css;
exports.sass = sass;
exports.js = js;
exports.html = html;

// 动态的监听和统一的运行
exports.default = series(base, commonHtml, reCss, css, sass, js, html, login, index, static, fonts, main, mainHtml, commonJs, sassBase, video);

watch('./src', series(this.default))